import 'package:flutter/material.dart';
import 'package:online_study/base/round_check_box.dart';
import 'package:online_study/mine/exam/exam_bean.dart';
import 'package:online_study/theme/colors.dart';
import 'package:online_study/theme/size.dart';

//考试的基本单元(一道题)
class ExamItemPage extends StatefulWidget {
  ExamBean _bean;

  ExamItemPage(this._bean);

  @override
  _ExamItemPageState createState() => _ExamItemPageState(_bean);
}

class _ExamItemPageState extends State<ExamItemPage> {
  ExamBean _bean;

  _ExamItemPageState(this._bean);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: ThemeColor.colorWhite,
      padding: EdgeInsets.symmetric(vertical: ThemeSize.height12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [_buildQuestion(), _buildOptions()],
      ),
    );
  }

  Widget _buildQuestion() {
    String type = "";
    if (_bean.isMulti) {
      type = "[多选]";
    } else {
      type = "[单选]";
    }
    String question = _bean.index.toString() + "." + type + _bean.question;
    return Container(
      padding: EdgeInsets.only(
          left: ThemeSize.width19,
          right: ThemeSize.width19,
          bottom: ThemeSize.height8),
      child: Text(question,
          style: TextStyle(
              color: ThemeColor.color333333,
              fontSize: ThemeSize.sp13,
              fontWeight: FontWeight.bold),
          softWrap: true),
    );
  }

  Widget _buildOptions() {
    return Column(
      children: _bean.answers
          .map((e) => Container(
                padding: EdgeInsets.only(
                    top: ThemeSize.height6, bottom: ThemeSize.height6),
                child: Row(
                  children: [
                    RoundCheckBox(
                        value: _bean.choiceAnswers.contains(e),
                        onChanged: (value) {
                          setState(() {
                            if (!value) {
                              _bean.choiceAnswers.remove(e);
                            } else {
                              if (!_bean.isMulti) {
                                _bean.choiceAnswers.clear();
                              }
                              _bean.choiceAnswers.add(e);
                            }
                          });
                        }),
                    Expanded(
                        child: Text(e,
                            style: TextStyle(
                                color: ThemeColor.color333333,
                                fontSize: ThemeSize.sp13),
                            softWrap: true))
                  ],
                ),
              ))
          .toList(),
    );
  }

  @override
  void didUpdateWidget(covariant ExamItemPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget._bean != oldWidget._bean) {
      setState(() {
        this._bean = widget._bean;
      });
    }
  }
}
